package cn.autumnorange.app.user.consumer.authorization.security.security.ajaxjsonlogin;

import cn.autumnorange.app.user.consumer.authorization.security.security.LoginTypeService;
import cn.autumnorange.app.user.consumer.authorization.system.LoginUserDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
public class AjaxJsonUsernamePasswordAuthenticationFilter
    extends AbstractAuthenticationProcessingFilter {

  protected LoginTypeService loginTypeService;

  public AjaxJsonUsernamePasswordAuthenticationFilter(
      String signInProcessingUrl, LoginTypeService loginTypeService) {
    //    "/是必须加的"
    super(new AntPathRequestMatcher(signInProcessingUrl, "POST"));
    this.loginTypeService = loginTypeService;
  }

  public Authentication attemptAuthentication(
      HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
    String contentType = request.getContentType();
    if (!request.getMethod().equals("POST")) {
      throw new AuthenticationServiceException(
          "Authentication method not supported: " + request.getMethod());
    } else if (StringUtils.trimToNull(contentType) == null) {
      throw new AuthenticationServiceException("contentType must has");
    } else if (!MediaType.APPLICATION_JSON_UTF8_VALUE.equalsIgnoreCase(contentType)
        && !MediaType.APPLICATION_JSON_VALUE.equalsIgnoreCase(contentType)) {
      throw new AuthenticationServiceException(
          "contentType must be"
              + MediaType.APPLICATION_JSON_VALUE
              + " or"
              + MediaType.APPLICATION_JSON_UTF8_VALUE);
    } else {
      LoginUserDto loginUserDto = loginTypeService.getLoginMsg(request);
      String username = loginUserDto.getUserName();
      String password = loginUserDto.getPassword();
      if (username == null) {
        username = "";
      }

      if (password == null) {
        password = "";
      }

      username = username.trim();
      UsernamePasswordAuthenticationToken authRequest =
          new UsernamePasswordAuthenticationToken(username, password);
      this.setDetails(request, authRequest);
      Cookie[] cookies = request.getCookies();

      System.out.println(  "登录校验过滤器AjaxJsonUsernamePasswordAuthenticationFilter获取cookie:");
      if (cookies != null) {
        for (Cookie c : cookies) {

          System.out.println(c.getName() + "--->" + c.getValue());
        }

      }else{
        System.out.println("登录校验过滤器AjaxJsonUsernamePasswordAuthenticationFilter获取cookie null");

      }
      return this.getAuthenticationManager().authenticate(authRequest);
    }
  }

  protected void setDetails(
      HttpServletRequest request, UsernamePasswordAuthenticationToken authRequest) {
    authRequest.setDetails(this.authenticationDetailsSource.buildDetails(request));
  }
}
